package 热题100;

import java.util.HashMap;
import java.util.Map;

public class 数组专题热题1两数之和 {
    public static void main(String[] args) {
        int[] arr = {3,2,4};
        int[] ints = twoSum(arr, 6);
        for (int i : ints) {
            System.out.println(i);
        }

    }

    public static int[] twoSum(int[] nums, int target) {
        int[] res = new int[2];
        int k = 0;
        Map<Integer,Integer> map = new HashMap<>();
        map.put(nums[0],0);
        for(int i = 1;i < nums.length;i++){
            //每次遍历，先比较当前的target - nums[i] = val ；map中是否包含val
            if(map.containsKey(target - nums[i])){
                //如果包含，给res赋值后退出
                res[0] = map.get(target - nums[i]);
                res[1] = i;
                return res;
            }
            //如果不包含，继续
            map.put(nums[i],i);
        }
        return res;
    }

}
